﻿using System;
using System.Collections.Generic;
using System.Linq;
using GeneralDFS.BusinessLogical.Config;
using GeneralDFS.Common;
using GeneralDFS.DataEntity.IO;
using GeneralDFS.DataEntity.Config;

namespace GeneralDFS.BusinessLogical.IO
{
    public class File
    {
        private FileNodeInfo _currentFile;
        private readonly int _blockSize;
        private DFSConfig _config;

        /// <summary>
        /// 创建一个新文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="path">路径</param>
        /// <param name="createrName">创建者名称</param>
        /// <param name="length">文件大小</param>
        /// <returns>创建的文件实例</returns>
        public static FileNodeInfo Create(int blocksize,string fileName,String path,String createrName,long length)
        {
            var node = new FileNodeInfo(fileName, path, DateTime.Now.LongNow(), createrName, length, null, blocksize);
            return node;
        }

        /// <summary>
        /// 初始化当前的操作文件
        /// </summary>
        /// <param name="node"></param>
        public File(DFSConfig config,FileNodeInfo node)
        {
            this._config = config;
            _currentFile = node;
            _blockSize = config.DetailConfig.BlockSize;
        }

        /// <summary>
        /// 设置当前操作文件(备用)
        /// </summary>
        /// <param name="file"></param>
        public void SetCurrentFile(FileNodeInfo file)
        {
            _currentFile = file;
        }

        public List<FileBlockInfo> BlockList()
        {
            return _currentFile.Blocks.ToList();
        }

        /// <summary>
        /// 添加文件块
        /// </summary>
        /// <param name="newBlock"></param>
        public void AddBlock(FileBlockInfo newBlock)
        {
            _currentFile.Blocks[newBlock.Offset/_blockSize] = newBlock;
        }

        /// <summary>
        /// 移除文件块
        /// </summary>
        /// <param name="guid"></param>
        public void RemoveBlock(Guid guid)
        {
            int index = Array.FindIndex(_currentFile.Blocks, info => info.Guid == guid);
            if (index >= 0)
                _currentFile.Blocks[index] = new FileBlockInfo();
        }

        /// <summary>
        /// 移除文件块
        /// </summary>
        /// <param name="oldBlock"></param>
        public void RemoveBlock(FileBlockInfo oldBlock)
        {
            int index = Array.FindIndex(_currentFile.Blocks, info => info.Guid == oldBlock.Guid);
            if (index >= 0)
                _currentFile.Blocks[index] = null;
        }

        /// <summary>
        /// 替换文件块
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="newBlock"></param>
        public void Replace(Guid guid, FileBlockInfo newBlock)
        {
            int index = Array.FindIndex(_currentFile.Blocks, info => info.Guid == guid);
            if (index >= 0)
                _currentFile.Blocks[index] = newBlock;
        }

        /// <summary>
        /// 通过文件块名称获取文件块
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public FileBlockInfo GetBlockByName(Guid guid)
        {
            return Array.Find(_currentFile.Blocks, info => info.Guid == guid);
        }
    }
}